package com.yz.lzq.liwushuo.ui.modules.gift.top100;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.yz.lzq.liwushuo.jsonbean.gift.GiftgoryTop100Bean;

import java.util.List;

/**
 * Created by AdminJia on 2016/12/12.
 */

public class GiftgoryTop100Presenter {
    private IGidftTop100Biz iGidftTop100Biz;
    private IgiftTop100View igiftTop100View;
    private GiftTop100Adapter adapter;
    private List<GiftgoryTop100Bean.DataBean.ItemsBean> items = null;
    private Context mContent = null;
    private ConnectivityManager manager;

    public GiftgoryTop100Presenter(IgiftTop100View igiftTop100View) {
        this.iGidftTop100Biz = new GidftTop100Biz();
        this.igiftTop100View = igiftTop100View;
    }

    public void getData(final Context _context, int offset) {
        iGidftTop100Biz.getData(new OnGiftTop100GetDataListener() {
            @Override
            public void getDataSuccess(List<GiftgoryTop100Bean.DataBean.ItemsBean> _items, String
                    imgUrl) {
                mContent = _context;
                items = _items;
                adapter = new GiftTop100Adapter(items, mContent);
                igiftTop100View.setImgUrl(imgUrl);
                igiftTop100View.setAdapterSuccess(adapter);

            }

            @Override
            public void getDataFaile(Throwable t) {
                igiftTop100View.faled(t);
            }
        }, offset);

    }

    public void getMoreData(final int offset) {
         checkNetworkState(offset);
//        iGidftTop100Biz.getMoreData(offset, new OnGiftTop100GetDataListener() {
//            @Override
//            public void getDataSuccess(List<GiftgoryTop100Bean.DataBean.ItemsBean> _items, String
//                    imgUrl) {
//                List<GiftgoryTop100Bean.DataBean.ItemsBean> itemsBeen = adapter.getmList();
//                int size = itemsBeen.size();
//                int size1 = _items.size();
//                if (offset == 0) {
////                    try {
////                        Thread.sleep(3000);
////                    } catch (InterruptedException e) {
////                        e.printStackTrace();
////                    }
//                    igiftTop100View.setImgUrl(imgUrl);
//                    adapter.reLoadData(_items, true);
//                } else {
//                    if (_items.size() != 0) {
//
//                        igiftTop100View.addItems(_items);
//
//                    } else {
//                        int m=offset;
//                        Toast.makeText(mContent, "没数据了", Toast.LENGTH_SHORT).show();
//                    }
//                }
//
//
////                adapter = new GiftTop100Adapter(items, context);
////                igiftTop100View.setAdapterSuccess(adapter, imgUrl);
//            }
//
//            @Override
//            public void getDataFaile(Throwable t) {
//                igiftTop100View.faled(t);
//            }
//        });
    }

    private boolean checkNetworkState(int offset) {
        boolean flag = false;
        //得到网络连接信息
        manager = (ConnectivityManager) mContent.getSystemService(Context.CONNECTIVITY_SERVICE);
        //去进行判断网络是否连接
        NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            flag = activeNetworkInfo.isAvailable();
        }
        if (!flag) {
            setNetwork();
        } else {
            isNetworkAvailable(offset);
        }

        return flag;
    }

    private void setNetwork() {
        Toast.makeText(mContent, "没网络啦", Toast.LENGTH_LONG).show();
    }

    private void isNetworkAvailable(int offset) {

        NetworkInfo.State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        NetworkInfo.State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING) {
            Toast.makeText(mContent, "你用的是gprs", Toast.LENGTH_SHORT).show();
            loadAdmob(offset);
        }
        //判断为wifi状态下才加载广告，如果是GPRS手机网络则不加载！
        if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
            Toast.makeText(mContent, "你用的是wifi", Toast.LENGTH_SHORT).show();
            loadAdmob(offset);
        }
    }

    private void loadAdmob(final int offset) {
        iGidftTop100Biz.getMoreData(offset, new OnGiftTop100GetDataListener() {
            @Override
            public void getDataSuccess(List<GiftgoryTop100Bean.DataBean.ItemsBean> _items, String
                    imgUrl) {
                List<GiftgoryTop100Bean.DataBean.ItemsBean> itemsBeen = adapter.getmList();
                int size = itemsBeen.size();
                int size1 = _items.size();
                if (offset == 0) {
                    igiftTop100View.setImgUrl(imgUrl);
                    adapter.reLoadData(_items, true);
                } else {
                    if (_items.size() != 0) {
                        adapter.additems(_items);

                    } else {
                        Toast.makeText(mContent, "没数据了", Toast.LENGTH_SHORT).show();
                    }
                }


//                adapter = new GiftTop100Adapter(items, context);
//                igiftTop100View.setAdapterSuccess(adapter, imgUrl);
            }

            @Override
            public void getDataFaile(Throwable t) {
                igiftTop100View.faled(t);
            }
        });
    }
}
